// pages/forgetPassword/forgetPassword.js
var api = require('../../utils/api.js')
var util = require('../../utils/util.js');
var md = require('../../utils/md5.js');
var app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    limit: 10,
    loadComplete:false,
    search:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var search=options.search;
    if (search!=undefined && search.length > 0) {
      this.setData({
        search: search
      })
      this.searchAction()
    }
  },

  bindSearchInput: function (e) {
    this.setData({
      search: e.detail.value
    })
  },

  // 搜索
  searchAction: function (e) {
    var that = this;
    if (that.data.search==undefined || that.data.search.length==0) {
      util.showError('搜索不能为空')
      return;
    }

    var params = {
      search: that.data.search,
      page:that.data.page,
      limit: that.data.limit
    };
    api.POST({
      params: params,
      url: app.globalData.coupons_search_url,
      success: function (res) {
        wx.stopPullDownRefresh()
        if (res.data.code=='') {
          that.setData({
            page: res.data.data.paging.page,
            loadComplete: (res.data.data.paging.page == res.data.data.paging.total_page) ? true : false,
            itemData: res.data.data.list
          })
        } else {
          if (res.data.message) {
            util.showError(res.data.message)
          } else {
            util.showError('出错了')
          }
        }
      },
      fail: function (fail) {
        wx.stopPullDownRefresh()
        util.showError("网络错误");
      }
    })

  },



  //页面下拉刷新
  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      page: 1,
      loadComplete: false
    });
    that.searchAction();
  },

  //页面上拉触底
  onReachBottom: function (e) {
    var that = this;
    if (that.data.itemData == undefined || that.data.itemData.length==0){
      return;
    }
    if (that.data.itemData.length>0 && !that.data.loadComplete){
      that.setData({
        page: (that.data.page)/1+1
      });
      that.searchAction();
    }
  },

})


